package com.suke.czx.modules.team.controller;

import com.google.common.collect.Lists;
import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.ParamValidateUtil;
import com.suke.czx.common.utils.R;
import com.suke.czx.common.validate.TeamSaveGroup;
import com.suke.czx.common.validate.TeamUpdateGroup;
import com.suke.czx.modules.sys.entity.SysUser;
import com.suke.czx.modules.team.entity.Team;
import com.suke.czx.modules.team.entity.TeamSearchBo;
import com.suke.czx.modules.team.service.TeamService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 应用模块名称: 团队
 * 代码描述: 团队控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/08 2:09
 */
@Validated
@RestController
@RequestMapping("/api/team")
@AllArgsConstructor
@Api(value = "TeamController", tags = "团队管理")
public class TeamController extends AbstractController {

    private final TeamService teamService;

    /**
     * 分页查询团队
     * @param teamSearchBo 查询BO
     * @return VO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:team:list')")
    public R list(TeamSearchBo teamSearchBo) {
        if (teamSearchBo.getPage() == null || teamSearchBo.getLimit() == null) {
            return R.error("缺少分页信息");
        }
        if (teamSearchBo.getTeamId() == null) {
            return R.error("团队编码不能为空");
        }
        // 分页查询信息列表
        return teamService.pageTeamList(teamSearchBo);
    }

    /**
     * 保存团队
     * @param team 团队
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("保存团队")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:team:save')")
    public R save(@Validated(TeamSaveGroup.class) Team team, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        team.setCreator(getUserId());
        team.setCreateTime(new Date());
        teamService.save(team);
        return R.ok();
    }

    /**
     * 更新团队
     * @param team 团队
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("更新团队")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:team:update')")
    public R update(@Validated(TeamUpdateGroup.class) Team team, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        team.setUpdater(getUserId());
        team.setUpdateTime(new Date());
        teamService.updateById(team);
        return R.ok();
    }

    /**
     * 删除团队成员
     * @param teamId 团队id
     * @param userId 员工id
     * @return VO
     */
    @SysLog("删除团队成员")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:team:delete')")
    public R delete(@NotNull(message = "团队编码不能为空") Long teamId, @NotNull(message = "员工编码不能为空") Long userId) {
        return teamService.updateDelTeam(teamId, userId, getUserId());
    }

    /**
     * 根据项目查询团队
     * @param projectId 项目编码
     * @return VO
     */
    @SysLog("根据项目查询团队")
    @RequestMapping(value = "/project", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:team:project')")
    public R team(@NotNull(message = "项目编码不能为空") Long projectId) {
        List<SysUser> userList = teamService.searchTeamByProjectId(projectId);
        return Objects.requireNonNull(R.ok().setData(Optional.ofNullable(userList).orElse(Lists.newArrayList())));
    }

}
